// 用于弹框的管理
<template>
  <div>
    <el-dialog
      :title="addInfo.isAdd ? '添加会员' : '编辑会员'"
      :visible.sync="addInfo.isShow"
      :before-close="cancel"
      center
    >
      <el-form :model="form" :rules="rules" ref="rules">
        <el-form-item
          label="手机号："
          :label-width="formLabelWidth"
          prop="phone"
        >
          <el-input v-model="form.phone" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="昵称："
          :label-width="formLabelWidth"
          prop="nickname"
        >
          <el-input v-model="form.nickname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item
          label="密码："
          :label-width="formLabelWidth"
        >
          <el-input v-model="form.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态：" :label-width="formLabelWidth">
          <el-switch
            v-model="form.status"
            active-color="#13ce66"
            inactive-color="#ff4949"
            :active-value="1"
            :inactive-value="2"
          >
          </el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
// 引入菜单添加接口文档
import { getMemberInfo, getMemberEdit } from "../../util/axios";
//引入辅助性函数
import { mapActions } from "vuex";
// console.log(indexRoutes,"路由配置项");   //结果为数组包对象
export default {
  data() {
    return {
      form: {
        nickname: "", // 昵称
        phone: "", //手机号
        password: "", //密码
        status: 1 //状态  1正常   2禁用
      },
      formLabelWidth: "120px", //label的宽度
      //表单验证规则
      rules: {
        nickname: [
          { required: true, message: "请输入昵称", trigger: "blur" },
        ],
        phone: [{ required: true, message: "请输入手机号", trigger: "blur" }]
      }
    };
  },
  props: ["addInfo"],
  methods: {
    // 调取接口
    ...mapActions({
      getMemberList: "member/getMemberListAction"
    }),
    //子传父关闭弹框,取消改变父组件的数据
    cancel() {
      // 关闭弹框
      this.$emit("cancel", false);
      //关闭弹框的同时重置弹框内容
      this.reset();
    },
    // 封装一个重置函数
    reset() {
      this.form = {
        nickname: "", // 昵称
        phone: "", //手机号
        password: "", //密码
        status: 1 //状态1正常2禁用
      };
      // 重置规则验证
      this.$refs["rules"].resetFields();
    },
    //封装一个查看一条数据的事件
    lookUp(uid) {
      // 调取查看一条数据的接口
      getMemberInfo({ uid }).then(res => {
        if (res.data.code == 200) {
          this.form = res.data.list;
          this.form.uid = uid;
        }
      });
    },
    update() {
      //调取添加接口
      getMemberEdit(this.form).then(res => {
        if (res.data.code == 200) {
          this.$message.success(res.data.msg);
          //重新调取接口
          this.getMemberList();
          //关闭弹框
          this.cancel();
        } else {
          this.$message.error(res.data.msg);
        }
      });
    }
  }
};
</script>

<style lang="" scoped></style>
